package edu.whu.notepal.controller;

import edu.whu.notepal.dto.Result;
import edu.whu.notepal.pojo.PrivateChat;
import edu.whu.notepal.pojo.User;
import edu.whu.notepal.service.NoteService;
import edu.whu.notepal.service.PrivateChatService;
import edu.whu.notepal.service.UserService;
import edu.whu.notepal.utils.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @description 便签发起的临时会话方法
 * @author 陈磊
 */
@RestController
@RequestMapping("/privateChat")
public class PrivateChatController {

    @Resource
    PrivateChatService privateChatService;

    @Resource
    RedisClient redisClient;

    @Resource
    MyWebSocket myWebSocket;

    @Resource
    NoteService noteService;

    @Resource
    UserService userService;

    @GetMapping("/{id}")
    public Result<PrivateChat> getById(@PathVariable("id")Long noteId){
        PrivateChat privateChat = redisClient.queryWithPassThrough(RedisConstants.PRIVATE_CHAT_INFO_KEY, noteId,
                PrivateChat.class, privateChatService::getById, RedisConstants.PRIVATE_CHAT_INFO_TTL, TimeUnit.MINUTES);
        return Result.success(privateChat);
    }

    @PostMapping("/add")
    public Result<String>addChat(Long noteId){
        Long id = UserHolder.getUser().getId();
        privateChatService.createPrivateChat(noteId, id);
        myWebSocket.sendToOne(SocketBeanFactory.instance
                (id,noteService.getPublisher(noteId),MessageType.PRIVATE_CHAT_ESTABLISHED,"已建立临时会话",noteId));
        return Result.success("已建立临时会话！");
    }

    @DeleteMapping("/exit")
    private Result<String> exitChat(Long noteId){
        privateChatService.deletePrivateChat(noteId);
        Long fromUser = UserHolder.getUser().getId();
        Long toUser = privateChatService.getTheOther(noteId,fromUser);
        myWebSocket.sendToOne(SocketBeanFactory.instance
                (fromUser,toUser,MessageType.PRIVATE_CHAT_CLOSED,"临时会话已关闭",noteId));
        return Result.success("临时会话已关闭！");
    }

    @GetMapping
    private Result<List<PrivateChat>> getMyChat(){
        return Result.success(privateChatService.getChatById(UserHolder.getUser().getId()));
    }

    @PostMapping("/friends")
    private Result<Integer> makeFriends(Long noteId){
        Integer code = privateChatService.makeFriends(noteId,UserHolder.getUser().getId());
        return Result.success(code);
    }

    @PostMapping("/messageCount")
    private Result<Integer>messageCount(Long noteId,int messageCount){
        PrivateChat privateChat = new PrivateChat();
        privateChat.setNoteId(noteId);
        privateChat.setMessageCount(messageCount);
        privateChatService.updateById(privateChat);
        return Result.success(privateChat.getMessageCount());
    }

    @GetMapping("/theOther")
    private Result<User>getTheOther(Long noteId){
        Long theOther = privateChatService.getTheOther(noteId, UserHolder.getUser().getId());
        return Result.success(userService.getById(theOther));
    }

}